<script>
  /**
   * 注意判断nums[ans] 与 target 比较
   * 小于 ans++
   */

  /**
   * @param {number[]} nums
   * @param {number} target
   * @return {number}
   */
  var searchInsert = function (nums, target) {
    let l = 0,
      r = nums.length - 1,
      ans;
    while (l <= r) {
      let mid = ((l + r) / 2) | 0;
      ans = mid;
      if (nums[mid] > target) {
        r = mid - 1;
      } else if (nums[mid] < target) {
        l = mid + 1;
      } else {
        return mid;
      }
    }
    if (nums[ans] < target) {
      ans++;
    }
    return ans;
  };
</script>
